package com.lwf.fatapi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lwf.fatapi.dto.CreateUserRequest;
import com.lwf.fatapi.entities.SysUser;
import com.lwf.fatapi.service.SysUserService;
import com.lwf.fatapi.utils.Result;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户控制器
 *
 * @author lwf
 */
@Tag(name = "用户管理", description = "系统用户管理相关接口")
@RestController
@RequestMapping("/system/users")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @Operation(summary = "创建用户")
    @PostMapping
    @SaCheckPermission("system:user:add")
    public Result createUser(@RequestBody @Validated CreateUserRequest request) {
        boolean result = userService.createUser(request);
        return result ? Result.success("用户创建成功") : Result.error("用户创建失败");
    }

    @Operation(summary = "更新用户")
    @PutMapping("/{id}")
    @SaCheckPermission("system:user:edit")
    public Result updateUser(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @RequestBody @Validated CreateUserRequest request) {
        boolean result = userService.updateUser(id, request);
        return result ? Result.success("用户更新成功") : Result.error("用户更新失败");
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:user:delete")
    public Result deleteUser(@Parameter(description = "用户ID") @PathVariable Long id) {
        boolean result = userService.deleteUser(id);
        return result ? Result.success("用户删除成功") : Result.error("用户删除失败");
    }

    @Operation(summary = "根据ID查询用户")
    @GetMapping("/{id}")
    @SaCheckPermission("system:user")
    public Result<SysUser> getUserById(@Parameter(description = "用户ID") @PathVariable Long id) {
        SysUser user = userService.getUserById(id);
        return Result.success(user);
    }

    @Operation(summary = "分页查询用户列表")
    @GetMapping("/page")
    @SaCheckPermission("system:user")
    public Result<Page<SysUser>> getUserPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int pageNo,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        Page<SysUser> result = userService.getUserPage(pageNo, pageSize, keyword);
        return Result.success(result);
    }

    @Operation(summary = "查询所有用户列表")
    @GetMapping("/list")
    @SaCheckPermission("system:user")
    public Result<List<SysUser>> getAllUsers() {
        List<SysUser> result = userService.getAllUsers();
        return Result.success(result);
    }

    @Operation(summary = "设置用户角色")
    @PostMapping("/{id}/roles")
    @SaCheckPermission("system:user:edit")
    public Result setUserRoles(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @RequestBody List<Long> roleIds) {
        boolean result = userService.setUserRoles(id, roleIds);
        return result ? Result.success("角色设置成功") : Result.error("角色设置失败");
    }

    @Operation(summary = "重置密码")
    @PostMapping("/{id}/reset-password")
    @SaCheckPermission("system:user:edit")
    public Result resetPassword(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "新密码") @RequestParam String newPassword) {
        boolean result = userService.resetPassword(id, newPassword);
        return result ? Result.success("密码重置成功") : Result.error("密码重置失败");
    }

    @Operation(summary = "修改密码")
    @PostMapping("/{id}/change-password")
    public Result changePassword(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "原密码") @RequestParam String oldPassword,
            @Parameter(description = "新密码") @RequestParam String newPassword) {
        boolean result = userService.changePassword(id, oldPassword, newPassword);
        return result ? Result.success("密码修改成功") : Result.error("密码修改失败");
    }

    @Operation(summary = "启用/禁用用户")
    @PostMapping("/{id}/status")
    @SaCheckPermission("system:user:edit")
    public Result toggleUserStatus(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        boolean result = userService.toggleUserStatus(id, status);
        return result ? Result.success("状态修改成功") : Result.error("状态修改失败");
    }
}
